package com.spzx.product.controller;

import com.github.pagehelper.PageHelper;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.domain.*;
import com.spzx.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 商品Controller
 */
@Tag(name = "商品接口管理")
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;



    @Operation(summary = "获取销量好的sku")
    @GetMapping("getTopSale")
    public R<List<ProductSku>> getTopSale() {
        return R.ok(productService.getTopSale());
    }

    //添加
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Product product) {
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 查询商品列表
     */
    @Operation(summary = "查询商品列表")
    @GetMapping("/list")
    public TableDataInfo list(Product product) {
        startPage();
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }

//    /**
//     * 获取商品详细信息
//     */
//    @Operation(summary = "获取商品详细信息")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id) {
//        return success(productService.selectProductById(id));
//    }

    /**
     * 修改商品
     * @param product
     * @return
     */
    @Operation(summary = "修改商品")
    @PutMapping
    public AjaxResult edit(@RequestBody Product product) {
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除商品
     *
     * @param ids
     * @return
     */
    @Operation(summary = "删除商品")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productService.deleteProductByIds(ids));
    }

    //远程调用使用，商品列表
    @GetMapping("/skuList/{pageNum}/{pageSize}")
    public R<TableDataInfo> skuList(@PathVariable Integer pageNum,
                                    @PathVariable Integer pageSize,
                                    @ModelAttribute SkuQuery skuQuery) {
        //设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        //调用service方法根据条件查询
        List<ProductSku> list = productService.selectProductSkuList(skuQuery);

        TableDataInfo dataTable = getDataTable(list);
        return R.ok(dataTable);
    }

    //////////////////////////////////////////////////
    //商品详情远程调用接口(自己写的!!!!!!)
    //接口1 :  根据skuId查询sku信息
    @Operation(summary = "获取sku信息")
    @GetMapping("/getProductSku/{skuId}")
    public R<ProductSku> getProductSku(@PathVariable(value = "skuId") Long skuId){
     ProductSku productSku =  productService.getProductSku(skuId);
        return  R.ok(productSku);
    }
    //接口2：根据商品id获取商品信息
    @GetMapping("/getProduct/{productId}")
    public  R<Product> getProductMessage(@PathVariable(value = "productId") Long productId){
        Product product = productService.getProduct(productId);
        return R.ok(product);
    }
    //接口3:根据商品id获取商品详细信息
    @GetMapping("/getProductDetails/{id}")
    public R<ProductDetails> getProductDetails(@PathVariable(value = "id") Long id){
        ProductDetails productDetails = productService.getProductDetails(id);
        return R.ok(productDetails);
    }
    //接口4：根据skuId获取商品sku最新价格信息
    @GetMapping("/getSkuPrice/{skuId}")
    public R<SkuPrice> getSkuPrice(@PathVariable(value = "skuId") Long skuId){
        SkuPrice skuPrice =  productService.getSkuPrice(skuId);
        return R.ok(skuPrice);
    }

    //接口5: 根据商品id查询商品sku规则信息
    @GetMapping("/getSkuSpecValue/{id}")
    public R<Map<String,Long>> getSkuSpecValue(@PathVariable(value = "id") Long id){
        return R.ok(productService.getProductSpecValue(id));
    }
    //接口6:获取sku对应库存数据
   @GetMapping("/getSkuStock/{skuId}")
    public R<SkuStockVo> getSkuStock(@PathVariable(value = "skuId") Long skuId){
        SkuStockVo skuStockVo = productService.getSkuStockVO(skuId);
        return R.ok(skuStockVo);
   }


}